package com.example;


import com.example.entity.User;
import com.example.mapper.UserMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.Md5Crypt;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;

/**
 * 该注解指定项目为springboot，由此类当作程序入口
 * ava  -jar network-storage-web-1.0.0.jar --spring.profiles.active=dev
 * 自动装配 web 依赖的环境
 * @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
**/
@SpringBootApplication
@MapperScan({"com.example.common.mapper","com.example.mapper"})
public class StarterApplication implements CommandLineRunner {

    @Resource
    private UserMapper userMapper;

    public static void main(String[] args) {
         SpringApplication.run(StarterApplication.class, args);
     }

    @Override
    public void run(String... args){
        try{
            if(userMapper != null){
           long userCount =  userMapper.count();
            if(userCount <= 0){
                System.out.println("正在初始化默认用户jonhon");
                User user = new User();
                user.setUsername("admin");
                String password = DigestUtils.md5Hex("111111").toUpperCase();
                user.setPassword(password);
                int rs = userMapper.insert(user);
                if(rs>0){
                    System.out.println("默认用户admin["+password+"]初始化成功");
                }
            }
        }
        }catch (Exception e){
             System.out.println("初始化异常。。。。。。");
            e.printStackTrace();
        }

    }
}
